#include "hc32_ddl.h"
/* Function control. Non-zero to enable. */
#define OTS_USE_TRIG                (0u)

#if (OTS_USE_TRIG > 0U)
    #define OTS_USE_INTERRUPT       (OTS_USE_TRIG)
#else
    #define OTS_USE_INTERRUPT       (0u)
#endif
/*
 * Definitions about OTS interrupt for the example.
 * OTS independent IRQn: [Int000_IRQn, Int031_IRQn], [Int110_IRQn, Int113_IRQn].
 */
#if (OTS_USE_INTERRUPT > 0u)
    #define OTS_INT_PRIO            (DDL_IRQ_PRIORITY_03)
    #define OTS_INT_SRC             (INT_OTS)
    #define OTS_IRQn                (Int113_IRQn)
#endif /* #if (OTS_USE_INTERRUPT > 0u) */
/* OTS clock selection. */
#define OTS_CLK_SEL_XTAL            (0u)
#define OTS_CLK_SEL_HRC             (1u)

/* Select XTAL as OTS clock. */
#define OTS_CLK_SEL                 (OTS_CLK_SEL_XTAL)

#define OTS_XTAL_K                  (737272.73f)
#define OTS_XTAL_M                  (27.55f)
#define OTS_HRC_K                   (3002.59f)
#define OTS_HRC_M                   (27.92f)
#if (OTS_USE_TRIG > 0U)
/**
 *******************************************************************************
 ** \brief  Specifies an event as the trigger source event of OTS.
 **
 ******************************************************************************/
static void OtsTrigConfig(void)
{
    /*
     * If a peripheral is used to generate the event which is used as a start trigger condition of OTS, \
     *   call the API of the peripheral to configure the peripheral.
     * The following operations are only used in this example.
     */
    stc_tim0_base_init_t stcTimerCfg;

    MEM_ZERO_STRUCT(stcTimerCfg);

    /* Timer0 peripheral enable */
    PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM02, Enable);

    /*config register for channel B */
    stcTimerCfg.Tim0_CounterMode     = Tim0_Sync;
    stcTimerCfg.Tim0_SyncClockSource = Tim0_Pclk1;
    stcTimerCfg.Tim0_ClockDivision   = Tim0_ClkDiv128;
    stcTimerCfg.Tim0_CmpValue        = 62500u - 1u;
    TIMER0_BaseInit(M4_TMR02, Tim0_ChannelB, &stcTimerCfg);

    /* Specifies event 'EVT_TMR02_GCMB' as the trigger source event of OTS. */
    PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_AOS, Enable);
    OTS_SetTriggerSrc(EVT_TMR02_GCMB);
}
#endif /* #if (OTS_USE_TRIG > 0U) */
float32_t f_temperature;
#if (OTS_USE_INTERRUPT > 0u)
static uint8_t m_u8OtsIntFlag = 0u;
#endif
static void OtsClockConfig(void)
{
#if (OTS_CLK_SEL == OTS_CLK_SEL_HRC)
    /* Enable HRC for OTS. */
    CLK_HrcCmd(Enable);
    /* Enable XTAL32 while clock selecting HRC. */
    CLK_Xtal32Cmd(Enable);
#else
    /* Enable XTAL for OTS. */
    CLK_XtalCmd(Enable);
#endif

    /* Enable LRC for OTS. */
    CLK_LrcCmd(Enable);
}
#if (OTS_USE_INTERRUPT > 0u)
static void OtsIrqCallback(void)
{
    f_temperature = OTS_CalculateTemp();
    m_u8OtsIntFlag = 1U;
}
static void OtsIrqConfig(void)
{
    stc_irq_regi_conf_t stcOtsIrqCfg;
    en_result_t         enIrqRegResult;

    stcOtsIrqCfg.enIntSrc    = OTS_INT_SRC;
    stcOtsIrqCfg.enIRQn      = OTS_IRQn;
    stcOtsIrqCfg.pfnCallback = &OtsIrqCallback;
    enIrqRegResult           = enIrqRegistration(&stcOtsIrqCfg);

    if (Ok == enIrqRegResult)
    {
        NVIC_ClearPendingIRQ(stcOtsIrqCfg.enIRQn);
        NVIC_SetPriority(stcOtsIrqCfg.enIRQn, OTS_INT_PRIO);
        NVIC_EnableIRQ(stcOtsIrqCfg.enIRQn);

        /* Enable the specified interrupts of OTS. */
        OTS_IntCmd(Enable);
    }
}
#endif
void OtsBaseConfig(void)
{
     stc_ots_init_t stcOtsInit;

    stcOtsInit.enAutoOff = OtsAutoOff_Disable;
#if (OTS_CLK_SEL == OTS_CLK_SEL_HRC)
    stcOtsInit.enClkSel   = OtsClkSel_Hrc;
    stcOtsInit.f32SlopeK  = OTS_HRC_K;
    stcOtsInit.f32OffsetM = OTS_HRC_M;
#else
    stcOtsInit.enClkSel   = OtsClkSel_Xtal;
    stcOtsInit.f32SlopeK  = OTS_XTAL_K;
    stcOtsInit.f32OffsetM = OTS_XTAL_M;
#endif

    /* 1. Enable OTS. */
    PWC_Fcg3PeriphClockCmd(PWC_FCG3_PERIPH_OTS, Enable);
    /* 2. Initialize OTS. */
    OTS_Init(&stcOtsInit);
#if (OTS_USE_INTERRUPT > 0U)
    OtsIrqConfig();
#endif

#if (OTS_USE_TRIG > 0U)
    OtsTrigConfig();
#endif
}

void User_OTS_Init(void)
{
    OtsBaseConfig();
    OtsClockConfig();
}

void Print_CPU_Temperature(void)
{
    
//    OTS_Polling(&f_temperature,500);
//    printf("CPU Temperature is %f\r\n", f_temperature);
}

